package cn.yh.register.order.api;

import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.AuthContextHolder;
import cn.yh.register.enums.OrderStatusEnum;
import cn.yh.register.model.order.OrderInfo;
import cn.yh.register.order.service.OrderService;
import cn.yh.register.vo.order.OrderCountQueryVo;
import cn.yh.register.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Resource
    private OrderService orderService;


    @ApiOperation("预约下单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public Result<Long> submitOrder(@ApiParam(name = "scheduleId", value = "排班id", required = true)
                                    @PathVariable String scheduleId,
                                    @ApiParam(name = "patientId", value = "就诊人id", required = true)
                                    @PathVariable Long patientId) {
        Long orderId = orderService.saveOrder(scheduleId, patientId);
        return Result.ok(orderId);
    }


    @ApiOperation("分页查询订单列表")
    @GetMapping("/auth/{page}/{limit}")
    public Result<IPage<OrderInfo>> findPageList(@PathVariable("page") Integer current,
                                                 @PathVariable Integer limit,
                                                 OrderQueryVo queryVo,
                                                 HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        //设置用户id
        queryVo.setUserId(userId);
        IPage<OrderInfo> pageList = orderService.findPageList(current, limit, queryVo);
        return Result.ok(pageList);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("/auth/getStatusList")
    public Result<List<Map<String, Object>> > getStatusList(){
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return Result.ok(statusList);
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/auth/getOrder/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable Long orderId){
        OrderInfo orderInfo = orderService.getOrderInfo(orderId);
        return Result.ok(orderInfo);
    }

    @ApiOperation("取消预约")
    @GetMapping("/auth/cancelOrder/{orderId}")
    public Result<Boolean>cancleOrder(@PathVariable Long orderId){
        Boolean flag = orderService.cancleOrder(orderId);
        return Result.ok(flag);
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("/inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }
}
